import { _decorator, Component, Node, find, Sprite, SpriteFrame, Animation, Label } from 'cc';
import { Constants } from "../data/Constants";
import { TopicManger } from "./TopicManger";
import { AudioManager } from "../audio/AudioManager";
const { ccclass, property } = _decorator;

@ccclass('ProgressMgr')
// static 静态
export class ProgressMgr extends Component {

    // @property({
    //     type: [Sprite],
    // })
    // public progress: Sprite[] = [];

    @property({ type: SpriteFrame })
    public progressBar1: SpriteFrame | null = null; // 进度条白色

    @property({ type: SpriteFrame })
    public progressBar2: SpriteFrame | null = null; // 进度条红色

    public static progressJinDu: Node = null
    public static progressBar: Sprite = null
    public static progressBar1: SpriteFrame = null
    public static progressBar2: SpriteFrame = null
    public static timerNum: Node = null

    public static passedTime: number = 0 // 进度时间
    public static totalTime: number = 5 // 总时间
    public static warningSign: Boolean = false // 紧急时刻
    public static timerOver = true
    public static isCombo = false

    start() {
        ProgressMgr.progressBar = find("Canvas/jindu/ProgressBar").getComponent(Sprite)
        ProgressMgr.progressJinDu = find("Canvas/jindu")
        ProgressMgr.timerNum = find("Canvas/jindu/time/Label")
        ProgressMgr.progressBar1 = this.progressBar1
        ProgressMgr.progressBar2 = this.progressBar2

    }

    update(dt: number) {
        // console.log(ProgressMgr.timerOver, ProgressMgr.passedTime, '1131321')
        if (ProgressMgr.timerOver) return
        if (ProgressMgr.passedTime < ProgressMgr.totalTime) {
            ProgressMgr.passedTime += dt
            const per = 1 - (ProgressMgr.passedTime / ProgressMgr.totalTime)

            ProgressMgr.progressBar.fillRange = per
            this.changeTimer(per)
            if (per < 0.4 && !ProgressMgr.warningSign) {
                this.warningSignPettrn()
            }
        } else {
            console.log('游戏结束')
            ProgressMgr.isCombo = false
            ProgressMgr.progressJinDu.getComponent(Animation).stop()
            ProgressMgr.timerOver = true
            // this.resetProgress()
            TopicManger.prototype.wrongAnswer()
        }
    }

    changeTimer(num) {
        const timerNum = Number((num * ProgressMgr.totalTime).toFixed(1)) > 0 ? Number((num * ProgressMgr.totalTime).toFixed(1)) : 0
        ProgressMgr.isCombo = (Number(ProgressMgr.totalTime) - timerNum) < 2 // 是否算是秒答
        ProgressMgr.timerNum.getComponent(Label).string = `${timerNum}`
    }

    warningSignPettrn() {
        // console.log('快要结束了')
        ProgressMgr.warningSign = true
        ProgressMgr.progressBar.spriteFrame = ProgressMgr.progressBar2
        ProgressMgr.progressJinDu.getComponent(Animation).play()
        AudioManager.playSound(Constants.AudioSource.djs);
    }

    resetProgress() {
        console.log('重置时间进度')
        ProgressMgr.progressBar.fillRange = 1
        ProgressMgr.progressBar.spriteFrame = ProgressMgr.progressBar1
        ProgressMgr.passedTime = 0
        ProgressMgr.totalTime = this.handleTotalTime()
        this.changeTimer(1)
        ProgressMgr.warningSign = false
        setTimeout(() => {
            ProgressMgr.timerOver = false
        }, 100)
    }

    handleTotalTime() {
        if (TopicManger.curScore >= 150) {
            return 3
        } else if (TopicManger.curScore >= 50 && TopicManger.curScore < 150) {
            return 4
        }
        return 5
    }



}
